* Logistic Discrete Time Hazard Model
insheet using "${data}\${inputFile}", names comma clear
set more off

foreach v of varlist * {
	capture confirm numeric variable `v'
		if !_rc {
		di "`v' is numeric"
		}
		else {
		di "`v' changed to numeric"
		cap drop temp
		cap gen temp=real(`v')
		cap drop `v'
		qui gen `v'=temp
		}
}
	drop if aum<=0
capture confirm new variable dd
if _rc==0{
gen dd = month // if DD months = calendar month (e.g. simulated data)
}
else {
drop if dd <= 4 // drop untra-short DD spells if provided 
}
///////////////////////////////////////////////////////////////////////////////
gsort fundid dd

gen logt=log(dd)

qui su aum
cap gen logaum =aum*0.06+(1-aum)*(-0.06) 
qui su idvol_style
cap gen id_prec=1/max(abs(r(p1)),idvol_style)

winsor xret_style, gen(wxret_style) p(0.005)
winsor id_prec, gen(wid_prec) p(0.005)
winsor cs_idvol_style, gen(wcs_idvol_style) p(0.005)


global softVars "af_fund af_coll soft_info"
global controls "fi_hat mkt mktlag_1_3m  mktlag_4_6m mktvol smbvol hmlvol umdvol"
global FEs "_Iyr*"
global sfx ""

if "${simData}"=="Yes" {
*** with data simulated from the model: 
global sfx "_simData"
* affiliation info is proxied by softinfo over the first 12-months  of DD
cap drop temp*
gen temp=(soft_info>0.4) if month<=12
bys fundid: egen temp2=mean(temp) 
bys fundid:  gen af_fund=(temp2>0.5)
cap drop temp*
gen temp=(soft_info>0)*(fundid/2==round(fundid/2,1)) if inrange(month,6,12)==1
bys fundid: egen af_coll=max(temp)
cap drop temp*

global controls "" // additional controls not relevant 
global FEs ""
}
/////////////////////////////////////////////////////////////////////////////////



global indicatelines `" Controls in all spec.&\multicolumn{4}{c}{Flows to allocator fund;  year fixed effects; and}	\\	"'						
global indicatelines `"${indicatelines} &\multicolumn{4}{c}{24-month rolling return and volty on Mkt, SMB, HML, UMD} \\ "'
global ordering  "wxret_style logaum wid_prec wcs_idvol_style ${softVars} logt dd"
global vcv "vce(cluster fundid)"

local data wxret_style wid_prec wcs_idvol_style logaum soft_info  // standardize
foreach var in `data' {
	egen m= mean(`var')
	egen s= sd(`var')
	replace `var'= (`var'- m)/s
	drop s m
}
cap label var wxret_style "Excess Returns"
cap label var wid_prec "\addlinespace Signal Informativeness"
cap label var wcs_idvol_style "Fund Quality Dispersion"
cap label var logaum "Log(AUM)"
cap label var af_fund "Affiliated fund (D)"
cap label var af_coll "Affiliated college (D)"
cap label var soft_info "\addlinespace Intensity"
cap label var dd "Duration"
cap label var logt "Log(Duration)"



eststo clear

eststo A1: logistic adm  c.logaum  c.(dd logt) ${controls} ${FEs}, nolog $vcv

eststo A2: logistic adm c.wxret_style c.logaum c.wid_prec c.wcs_idvol_style c.(dd logt) ${controls} ${FEs}, nolog $vcv
	test (wid_prec= 0) (wcs_idvol_style= 0) 
	estadd scalar wald_stat= r(chi2)
	estadd scalar wald_pval= r(p)
	estout, stats(wald_stat wald_pval)
	
eststo A3: logistic adm c.wxret_style c.logaum c.wid_prec c.wcs_idvol_style af_fund af_coll c.(dd logt) ///
	${controls} ${FEs}, nolog $vcv
	test (af_fund= 0) (af_coll= 0)
	estadd scalar wald_stat= r(chi2)
	estadd scalar wald_pval= r(p)
	estout, stats(wald_stat wald_pval)
	
eststo A4: logistic adm c.wxret_style c.logaum c.wid_prec c.wcs_idvol_style af_fund af_coll c.soft_info c.(dd logt) ///
	${controls} ${FEs}, nolog $vcv
	test (soft_info= 0)
	estadd scalar wald_stat= r(chi2)
	estadd scalar wald_pval= r(p)
	estout, stats(wald_stat wald_pval)

esttab A1 A2 A3 A4 using "${output}\Table5${sfx}.tex", replace booktabs eform frag  order(${ordering}) ///
star(* 0.10 ** 0.05 *** 0.01) mlabels(none) nonotes eqlabel(none) num label drop(_cons, relax) subs("\_" "_") ///
stats(N wald_stat wald_pval, fmt(%15.0fc 2 4) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
labels(`"\addlinespace Observations"' `"\(F\)-stat (\emph{added variables})"' `"\(p\)-value"')) ///
refcat(wxret_style "\addlinespace\quad\emph{Public information:}" ///
	   af_fund "\addlinespace\quad\emph{Private information:}" ///
	   logt "\addlinespace\quad\emph{Due-diligence spell:}", nolabel) ///
collabels(none) cells("b(fmt(3)star)" "t(fmt(2)par([ ]))") ///
preh(\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{@M}{d{3.6}}}\toprule) ///
posth(\cmidrule(lr){2-5}\addlinespace) pref(${indicatelines}) postf(\bottomrule\end{tabular})

